<?php
declare(strict_types=1);

namespace App\Console\Commands\Tasks;

use App\Repositories\LogRepository;
use App\Repositories\Streamer\StreamerApplyRepository;
use App\Repositories\Streamer\StreamerTaskRecordRepository;
use App\Repositories\UserRepository;
use Illuminate\Console\Command;

class StreamerTaskOnlineStatCommand extends Command
{
    //命令名 php artisan task:streamer_task_online_stat
    protected $signature = 'task:streamer_task_online_stat';

    //命令描述
    protected $description = 'streamer task online stat';

    public function getSignature()
    {
        return $this->signature;
    }

    public function __construct()
    {
        parent::__construct();
    }

    //业务处理
    public function handle()
    {
        $userIds =  StreamerApplyRepository::getInstance()->streamers();
        foreach ($userIds as $userId){
            $log = New LogRepository("streamer-task-online-stat");
            $log->log("user_id", $userId);
            try{
                $second = UserRepository::getInstance()->userTodayOnlineSecond($userId);
                StreamerTaskRecordRepository::getInstance()->onlineProgress($userId, $second);
                // 调试
                $log->log("second", $second);
                $log->setOutputTypeStdout();
                $log->Output();
            }catch (\Exception $e){
                // 异常日志
                $log->logs([
                    "message" => $e->getMessage(),
                    "trace" => $e->getTraceAsString(),
                ]);
                $log->setOutputTypeFile();
                $log->Output();
                continue;
            }

        }

    }

}